package org.example.gui;

import lombok.AllArgsConstructor;
import lombok.Getter;

import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.util.Arrays;

/**
 * 图片类型过滤器
 */
@Getter
class PictureTypeFilter extends FileFilter {

    /**
     * 初始化默认值
     */
    private final Type type;

    public PictureTypeFilter(Type type) {
        this.type = type;
    }

    @Override
    public boolean accept(File file) {
        return contains(file.toString().toLowerCase());
    }

    @Override
    public String getDescription() {
        return String.format("*.%s(%s图像)", type.getDesc(), type.getDesc());
    }

    @Getter
    @AllArgsConstructor
    enum Type {
        PNG(".png", "PNG"),
        JPG(".jpg", "JPG"),
        BMP(".bmp", "BMP"),
        GIF(".gif", "GIF");

        private final String suffix;
        private final String desc;
    }

    public static boolean contains(String type) {
        return type.contains(Type.PNG.suffix) ||
                type.contains(Type.JPG.suffix) ||
                type.contains(Type.BMP.suffix) ||
                type.contains(Type.GIF.suffix);
    }

    public static FileFilter generator(String type) {
        Type imageType = Arrays.stream(Type.values())
                .filter(v -> v.desc.equals(type)).findFirst()
                .orElseThrow(() -> new RuntimeException("不存在的图片类型"));
        return generator(imageType);
    }

    public static FileFilter generator(Type type) {
        return new PictureTypeFilter(type);
    }
}
